﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using IWshRuntimeLibrary;

namespace XbdSoft.WPFVersion
{
    public class XbdConfiguration
    {
        private const string SaveFilePathElementName = "saveFilePath";
        private const string MaxDownloadNumElementName = "maxDownloadNum";
        private const string AutoStartElementName = "autoStart";

        public string SaveFilePath { get; set; }
        public int MaxDownloadNum { get; set; }
        public bool AutoStart { get; set; }


        protected XbdConfiguration()
        {
            this.SaveFilePath = @"D:\xbddownload";
            MaxDownloadNum = 4;
            AutoStart = false;
        }

        public static XbdConfiguration Load()
        {
            string exeFilePath = GetXbdSoftExeDirectory();
            var xDocument = XDocument.Load(System.IO.Path.Combine(exeFilePath, "configuration.xml"));
            var rootElement = xDocument.Root;
            var configuration = new XbdConfiguration();
            try
            {
                configuration.SaveFilePath = rootElement.Element(SaveFilePathElementName).Value;
                try
                {
                    var numStr = rootElement.Element(MaxDownloadNumElementName).Value;
                    configuration.MaxDownloadNum = Convert.ToInt32(numStr);
                }
                catch { }
                configuration.AutoStart = rootElement.Element(AutoStartElementName).Value == "1";
            }
            catch {
                MessageBox.Show("配置文件 configuration.xml 格式不正确.");
            }
            return configuration;
        }

        private static string GetXbdSoftExeDirectory()
        {
            return Environment.CurrentDirectory;

            var registerPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\XbdSoft";
            var exeFilePath = Registry.GetValue(registerPath, "EXE", string.Empty).ToString();
            return Path.GetDirectoryName(exeFilePath);
        }
        
        private static void SetElementValue(XElement rootElement, XName childElementName, string value)
        {
            var childElement = rootElement.Element(childElementName);
            if (childElement == null)
            {
                childElement = new XElement(childElementName);
                rootElement.Add(childElement);
            }
            childElement.Value = value ?? String.Empty;
        }

        public static void Save(XbdConfiguration configuration)
        {
            var xDocument = XDocument.Load(System.IO.Path.Combine(Environment.CurrentDirectory, "configuration.xml"));
            SetElementValue(xDocument.Root, SaveFilePathElementName, configuration.SaveFilePath );
            SetElementValue(xDocument.Root, MaxDownloadNumElementName, configuration.MaxDownloadNum.ToString());
            SetElementValue(xDocument.Root, AutoStartElementName, configuration.AutoStart ? "1" : "0");
            xDocument.Save(System.IO.Path.Combine(Environment.CurrentDirectory, "configuration.xml"));
        }

       

    }
}
